package shaman.storage.impl.file.date;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;

public class UnixDateFormat extends DateFormat {

    private static final DateFormatElement[] fields = new DateFormatElement[]{
        new DateFormatElement(Calendar.YEAR, 4),
        new DateFormatElement(Calendar.MONTH, 2, 1),
        new DateFormatElement(Calendar.DAY_OF_MONTH, 2),
        new DateFormatElement(Calendar.HOUR_OF_DAY, 2),
        new DateFormatElement(Calendar.MINUTE, 2),
        new DateFormatElement(Calendar.SECOND, 2, "+"),
        new DateFormatElement(Calendar.MILLISECOND, 3, ".")};
    private static final UnixDateFormat instance = new UnixDateFormat();

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        for (DateFormatElement el : fields) {
            el.format(cal, toAppendTo);
        }
        return toAppendTo;
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void format(Date date, StringBuffer toFormatTo) {
        instance.format(date, toFormatTo, null);
    }
}
